﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Common.Entity;
using ESOL.Business.Mst;
using ESOL.Business.Prod;
using ESOL.Common;

namespace ESOL.WinUI.Manufacture
{
    public partial class frmMaintainCooperation : BaseForm.frmBaseDialogyMaintainForm
    {
        #region vars
        private ProdProductTechnicEntity mSelectedProductTechnic;
        private ProdPlanOrderItemEntity mSelectedPlanOrderItem;
        private ProdCooperationOrderEntity mSelectedCooperationOrder;
        #endregion

        #region ctor
        public frmMaintainCooperation(ProdProductTechnicEntity entity)
        {
            InitializeComponent();
            this.mSelectedProductTechnic = entity;
            mSelectedPlanOrderItem = ProdPlanOrderItemBs.GetProdPlanOrderItemByID(entity.PlanOrderItemId);
        }
        public frmMaintainCooperation(ProdCooperationOrderEntity entity)
        {
            InitializeComponent();
            mSelectedCooperationOrder = entity;
            ProdProductTechnicEntity mCooperationProductTechnic = ProdProductTechnicBs.GetProdProductTechnicByID(entity.ProductTechnicId);
            if (mCooperationProductTechnic == null)
                return;
            mSelectedPlanOrderItem = ProdPlanOrderItemBs.GetProdPlanOrderItemByID(mCooperationProductTechnic.PlanOrderItemId);
            if (mSelectedPlanOrderItem == null)
                return;
            DataTable dt = ProdProductTechnicBs.GetDtItemFlowStationByItemID(mSelectedPlanOrderItem.PlanOrderItemId);
            if (dt.Rows.Count == 0)
                return;
            DataRow[] arryDataRow = dt.Select("PRODUCT_TECHNIC_ID='" + mSelectedCooperationOrder.ProductTechnicId + "'");
            if (arryDataRow == null)
                return;
            int nIndex = dt.Rows.IndexOf(arryDataRow[0]);
            if (nIndex == 0)
                return;
            mSelectedProductTechnic = ProdProductTechnicBs.GetProdProductTechnicByID(dt.Rows[nIndex - 1]["PRODUCT_TECHNIC_ID"].ToString());
            if (mSelectedProductTechnic == null)
                return;

        }
        public frmMaintainCooperation()
        {
            InitializeComponent();
        }
        #endregion

        #region functions
        private void Init()
        {
            if (ThisActionType == ActionType.New || ThisActionType == ActionType.Modify)
            {
                btnSave.Enabled = true;
            }
            if (ThisActionType == ActionType.View)
            {
                btnSave.Enabled = false;
            }
        }
        private void BindCombobox()
        {
            if (mSelectedPlanOrderItem != null)
            {
                DataTable dt = ProdProductTechnicBs.GetDtItemFlowStationByItemID(mSelectedPlanOrderItem.PlanOrderItemId);
                cmbCurProductTechnic.DisplayMember = "TECHNIC_NAME";
                cmbCurProductTechnic.ValueMember = "PRODUCT_TECHNIC_ID";
                cmbCurProductTechnic.DataSource = dt.Copy();
                cmbCooperationTechnic.DisplayMember = "TECHNIC_NAME";
                cmbCooperationTechnic.ValueMember = "PRODUCT_TECHNIC_ID";
                cmbCooperationTechnic.DataSource = dt.Copy();
            }
            DataTable dtStatus = new DataTable();
            dtStatus.Columns.Add("name");
            dtStatus.Columns.Add("value", typeof(int));
            dtStatus.Rows.Add("新增", 0);
            dtStatus.Rows.Add("外协中", 1);
            dtStatus.Rows.Add("完成", 2);
            cmbStatus.DataSource = dtStatus;
            cmbStatus.DisplayMember = "name";
            cmbStatus.ValueMember = "value";
            //string sCondition = " and s.STATUS='STATUS03'";
            DataTable dtSupplier = MstSupplierBs.GetMstSupplierByCondition("", "", "");
            DataRow drSupplier = dtSupplier.NewRow();
            dtSupplier.Rows.Add(drSupplier);
            cmbSupplier.DisplayMember = "SUPPLIER_NAME";
            cmbSupplier.ValueMember = "SUPPLIER_ID";
            cmbSupplier.DataSource = dtSupplier;
        }

        private void SetField()
        {
            if (ThisActionType == ActionType.New)
            {
                if (mSelectedPlanOrderItem != null)
                {
                    txtWorkingOrder.Text = mSelectedPlanOrderItem.OrderItemCode;
                    cmbCurProductTechnic.SetSelectedValue(mSelectedProductTechnic.ProductTechnicId);
                    DataTable dtCurProductTechnic = cmbCurProductTechnic.DataSource as DataTable;
                    DataRow[] drCurProductTechnic = dtCurProductTechnic.Select("PRODUCT_TECHNIC_ID='" + mSelectedProductTechnic.ProductTechnicId + "'");
                    txtCurTechnicCode.Text = drCurProductTechnic[0]["TECHNIC_CODE"].ToString();
                    MstProductEntity mProduct = MstProductBs.GetMstProductTypeByID(mSelectedPlanOrderItem.ProductId);
                    txtProductName.Text = mProduct.ProductName;
                    txtProductCode.Text = mProduct.ProductCode;
                    txtProductDescription.Text = mProduct.Description;
                    txtPlanQty.Text = mSelectedPlanOrderItem.Qty.ToString();

                    int nIndex = dtCurProductTechnic.Rows.IndexOf(drCurProductTechnic[0]);
                    DataRow drCooperationTechnic = dtCurProductTechnic.Rows[nIndex];//*
                    cmbCooperationTechnic.SetSelectedValue(drCooperationTechnic["PRODUCT_TECHNIC_ID"].ToString());
                    txtCooperationTechnicCode.Text = drCooperationTechnic["TECHNIC_CODE"].ToString();
                    cmbStatus.SetSelectedValue("0");//新增
                }
            }
            else if (ThisActionType == ActionType.Modify || ThisActionType == ActionType.View)
            {
                if (mSelectedCooperationOrder == null)
                    return;
                txtWorkingOrder.Text = mSelectedPlanOrderItem.OrderItemCode;
                cmbCurProductTechnic.SetSelectedValue(mSelectedProductTechnic.ProductTechnicId);
                DataTable dtCurProductTechnic = cmbCurProductTechnic.DataSource as DataTable;
                DataRow[] drCurProductTechnic = dtCurProductTechnic.Select("PRODUCT_TECHNIC_ID='" + mSelectedProductTechnic.ProductTechnicId + "'");
                txtCurTechnicCode.Text = drCurProductTechnic[0]["TECHNIC_CODE"].ToString();
                MstProductEntity mProduct = MstProductBs.GetMstProductTypeByID(mSelectedPlanOrderItem.ProductId);
                txtProductName.Text = mProduct.ProductName;
                txtProductCode.Text = mProduct.ProductCode;
                txtProductDescription.Text = mProduct.Description;
                txtPlanQty.Text = mSelectedPlanOrderItem.Qty.ToString();

                cmbCooperationTechnic.SetSelectedValue(mSelectedCooperationOrder.ProductTechnicId);
                ProdProductTechnicEntity mProductTechnic = ProdProductTechnicBs.GetProdProductTechnicByID(mSelectedCooperationOrder.ProductTechnicId);
                ProdTechnicEntity mTechnic = ProdTechnicBs.GetProdTechnicByID(mProductTechnic.TechnicId);
                txtCooperationTechnicCode.Text = mTechnic.TechnicCode;
                dtpRequiredDatetime.EditValue = mSelectedCooperationOrder.PlanDeliveryDate;
                txtQty.Text = mSelectedCooperationOrder.Qty.ToString();
                txtWeight.Text = mSelectedCooperationOrder.Weight.ToString();
                cmbStatus.SetSelectedValue(mSelectedCooperationOrder.Status);
                txtRemark.Text = mSelectedCooperationOrder.Remark;
                cmbSupplier.SetSelectedValue(mSelectedCooperationOrder.SupplierId);
                txtUnitPrice.Text = mSelectedCooperationOrder.UnitPrice.ToString();
                txtTotalPrice.Text = mSelectedCooperationOrder.TotalPrice.ToString();

                UICommon.UICommonFunctions.SetTextBoxDate(txtCreatedDate, mSelectedCooperationOrder.CreatedDate, "yyyy-MM-dd");
                UICommon.UICommonFunctions.SetTextBoxPersonName(txtCreatedBy, mSelectedCooperationOrder.CreatedBy);
                UICommon.UICommonFunctions.SetTextBoxDate(txtLastModifiedDate, mSelectedCooperationOrder.LastModifiedDate, "yyyy-MM-dd");
                UICommon.UICommonFunctions.SetTextBoxPersonName(txtLastModifiedBy, mSelectedCooperationOrder.LastModifiedBy);
            }
        }

        private bool CheckData()
        {
            if (string.IsNullOrEmpty(cmbCooperationTechnic.SelectedValue.ToString()))
            {
                MessageBox.Show("外协工序不可为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(cmbSupplier.SelectedValue.ToString()))
            {
                MessageBox.Show("外协厂商不可为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(txtQty.Text))
            {
                MessageBox.Show("外协数量不可为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (dtpRequiredDatetime.EditValue == null)
            {
                MessageBox.Show("计划交期不可为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(txtUnitPrice.Text))
            {
                MessageBox.Show("外协单价不可为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(txtTotalPrice.Text))
            {
                MessageBox.Show("外协总价不可为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return true;
        }

        private void SetEntity()
        {
            if (ThisActionType == ActionType.New)
            {
                mSelectedCooperationOrder = new ProdCooperationOrderEntity();
                mSelectedCooperationOrder.OrderId = Guid.NewGuid().ToString();
                mSelectedCooperationOrder.ProductTechnicId = cmbCooperationTechnic.SelectedValue.ToString();
                mSelectedCooperationOrder.Status = Convert.ToInt32(cmbStatus.SelectedValue.ToString());
                mSelectedCooperationOrder.SupplierName = cmbSupplier.Text;
                mSelectedCooperationOrder.SupplierId = cmbSupplier.SelectedValue.ToString();
                mSelectedCooperationOrder.Qty = Convert.ToDecimal(txtQty.Text);
                if (string.IsNullOrEmpty(txtWeight.Text) == false)
                    mSelectedCooperationOrder.Weight = Convert.ToDecimal(txtWeight.Text);
                mSelectedCooperationOrder.PlanDeliveryDate = Convert.ToDateTime(dtpRequiredDatetime.EditValue.ToString());
                mSelectedCooperationOrder.Remark = txtRemark.Text;
                mSelectedCooperationOrder.CreatedBy = Common.CommonParameter.CurrentUser.PersonId;
                mSelectedCooperationOrder.CreatedDate = DateTime.Now;
                mSelectedCooperationOrder.LastModifiedBy = Common.CommonParameter.CurrentUser.PersonId;
                mSelectedCooperationOrder.LastModifiedDate = DateTime.Now;
                mSelectedCooperationOrder.UnitPrice = Convert.ToDecimal(txtUnitPrice.Text);
                mSelectedCooperationOrder.TotalPrice = Convert.ToDecimal(txtTotalPrice.Text);
            }
            else if (ThisActionType == ActionType.Modify)
            {
                mSelectedCooperationOrder.ProductTechnicId = cmbCooperationTechnic.SelectedValue.ToString();
                mSelectedCooperationOrder.SupplierName = cmbSupplier.Text;
                mSelectedCooperationOrder.SupplierId = cmbSupplier.SelectedValue.ToString();
                mSelectedCooperationOrder.Qty = Convert.ToDecimal(txtQty.Text);
                if (string.IsNullOrEmpty(txtWeight.Text) == false)
                    mSelectedCooperationOrder.Weight = Convert.ToDecimal(txtWeight.Text);
                mSelectedCooperationOrder.PlanDeliveryDate = Convert.ToDateTime(dtpRequiredDatetime.EditValue.ToString());
                mSelectedCooperationOrder.Remark = txtRemark.Text;
                mSelectedCooperationOrder.LastModifiedBy = Common.CommonParameter.CurrentUser.PersonId;
                mSelectedCooperationOrder.LastModifiedDate = DateTime.Now;
                mSelectedCooperationOrder.UnitPrice = Convert.ToDecimal(txtUnitPrice.Text);
                mSelectedCooperationOrder.TotalPrice = Convert.ToDecimal(txtTotalPrice.Text);
            }
        }

        private bool Save()
        {
            if (mSelectedCooperationOrder == null)
            {
                MessageBox.Show("保存失败，外协单不存在！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (ThisActionType == ActionType.New)
            {
                if (ProdCooperationOrderBs.SaveCooperationOrder(true, mSelectedCooperationOrder) == false)
                {
                    MessageBox.Show("保存失败，外协单不存在！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
            }
            else if (ThisActionType == ActionType.Modify)
            {
                if (ProdCooperationOrderBs.SaveCooperationOrder(false, mSelectedCooperationOrder) == false)
                {
                    MessageBox.Show("更新失败，外协单不存在！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
            }
            return true;
        }
        #endregion

        #region events
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            Init();
            BindCombobox();
            SetField();
        }

        protected override void Click_Save()
        {
            if (CheckData() == false)
                return;
            SetEntity();
            if (Save())
            {
                MessageBox.Show("保存成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                DialogResult = DialogResult.OK;
                return;
            }
        }

        private void txtUnitPrice_EditValueChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtUnitPrice.Text))
                return;
            if (string.IsNullOrEmpty(txtQty.Text))
                return;
            txtTotalPrice.Text = (Convert.ToDecimal(txtQty.Text) * Convert.ToDecimal(txtUnitPrice.Text)).ToString();
        }

        private void txtQty_EditValueChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtUnitPrice.Text))
                return;
            if (string.IsNullOrEmpty(txtQty.Text))
                return;
            txtTotalPrice.Text = (Convert.ToDecimal(txtQty.Text) * Convert.ToDecimal(txtUnitPrice.Text)).ToString();
        }

        private void txtWorkingOrder_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainWorkingOrderQuery frm = new frmMaintainWorkingOrderQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedProdPlanOrderItem != null)
            {
                //获取工令单当前报工工序
                mSelectedPlanOrderItem = frm.SelectedProdPlanOrderItem;
                ProdProductTechnicEntity mCurProductTechnic = ProdProductTechnicBs.GetProductTechnicByPlanOrderItemID(mSelectedPlanOrderItem.PlanOrderItemId);
                if (mCurProductTechnic == null)
                {
                    MessageBox.Show("当前工令单尚未报工！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    mSelectedProductTechnic = mCurProductTechnic;
                    BindCombobox();
                    SetField();
                }
            }
        }
        #endregion
    }
}
